package com.xxl.job.executor.kettle.jobHandler;

import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.TriggerParam;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.kettle.dto.RepositoryDTO;
import com.xxl.job.executor.kettle.enums.RunTypeEnum;
import com.xxl.job.executor.kettle.execute.JobExecute;
import com.xxl.job.executor.kettle.execute.TransExecute;
import com.xxl.job.executor.kettle.util.RepositoryUtil;
import org.apache.commons.lang.StringUtils;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogLevel;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author songsong
 * @version 1.0
 * @date 2022/12/1 15:21
 **/
@Component
public class KettleJobHandler {

    @XxlJob("kettleJobHandler")
    public void execute(TriggerParam triggerParam) throws Exception{
        System.out.println("传递的参数：" + JSONObject.toJSON(triggerParam));
        // 设置执行参数
        Map<String, String> params = new HashMap<>(2);
        if (StringUtils.isNotEmpty(triggerParam.getExecutorParams())) {
            params.putAll(JSONObject.parseObject(triggerParam.getExecutorParams(), Map.class));
        }
        // 判断是执行资源库还是执行文件脚本
        // 执行作业并返回日志
        String logText = "";
//        String recordId = IdUtil.simpleUUID();
//        try {
            RepositoryDTO repositoryDTO = JSONObject.parseObject(triggerParam.getRepositoryStr(),RepositoryDTO.class);
            switch (RunTypeEnum.getEnum(triggerParam.getExecuteType())) {
                case REP:
                    if (triggerParam.getType() == "0" || triggerParam.getType().equals("0")) {
                        logText = JobExecute.run(RepositoryUtil.connection(repositoryDTO)
                                , triggerParam.getPath(), triggerParam.getName()
                                , null, params
                                , LogLevel.BASIC);
                    } else {
                        logText = TransExecute.run(RepositoryUtil.connection(repositoryDTO)
                                , triggerParam.getPath(), triggerParam.getName()
                                , null, params
                                , LogLevel.BASIC, triggerParam.getJobId()+"");
                    }
                    break;
                case FILE:
                    if (triggerParam.getType() == "0" || triggerParam.getType().equals("0")) {
                        logText = JobExecute.run(triggerParam.getFileByte(), null
                                , LogLevel.BASIC);
                    } else {
                        logText = TransExecute.run(triggerParam.getFileByte(), null
                                , LogLevel.BASIC, triggerParam.getJobId()+"");
                    }
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + RunTypeEnum.getEnum(triggerParam.getExecuteType()));
            }
            XxlJobHelper.log("<br>----------- xxl-job kettle job execute log -----------<br>----------- :" + logText);
//        } catch(KettleException e) {
//            String msg = "执行失败";
//            logText = e.getMessage();
//            XxlJobHelper.log("<br>----------- xxl-job kettle job execute error log -----------<br>----------- :" + logText);
//        } catch (Exception e) {
//            e.printStackTrace();
//            XxlJobHelper.log("<br>----------- xxl-job kettle job execute error log -----------<br>----------- :" + e.getMessage());
//        }
    }
}
